﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Runda.CMS.Common.Log;
using Runda.CMS.Bll;

namespace Runda.CMS.Service.RundaService
{
    public class BillProcessor
    {
        private int ThreadCount;
        private bool IsRunning;
        private List<System.Threading.Thread> ThreadList;   //线程列表                     
        private int Interval = 2 * 60 * 60 * 1000; //时间间隔2小时

        public BillProcessor(int threadCount)
        {
            this.ThreadCount = threadCount;
            this.ThreadList = new List<System.Threading.Thread>();
            this.ThreadList.Add(new System.Threading.Thread(Oper));                     
        }

        public void Start()
        {
            if (IsRunning)
                return;

            IsRunning = true;
            foreach (System.Threading.Thread thread in this.ThreadList)
            {
                //启动所有线程
                thread.Start();
                System.Threading.Thread.Sleep(100);
            }
        }

        public void Sotp()
        {
            IsRunning = false;

            System.Threading.Thread.Sleep(1000);

            foreach (System.Threading.Thread thread in this.ThreadList)
            {
                try
                {
                    thread.Abort();
                }
                catch
                {
                }

            }
        }
           
        private void Oper()
        {
            while (IsRunning)
            {
                try
                {
                    new BillBll().SetBillPenaltyAwardForBusinessor(new DateTime(System.DateTime.Now.AddMonths(1).Year, System.DateTime.Now.AddMonths(1).Month, 1).AddDays(-1));
                          
                    //时间间隔
                    System.Threading.Thread.Sleep(Interval);
                    //System.DateTime tommorow = new DateTime(DateTime.Now.AddDays(1).Year, DateTime.Now.AddDays(1).Month, DateTime.Now.AddDays(1).Day, 14, 30, 0);
                    //System.Threading.Thread.Sleep(tommorow.Subtract(DateTime.Now));
                }
                catch (Exception ex)
                {
                    
                }
            }
        }
    }
}
